package com.sansecy.customview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.Spinner;

public class SeekBarActivity extends AppCompatActivity {
    private String currentWidget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_bar);

        Button increaseBtn = findViewById(R.id.button_increase);
        Button reduceBtn = findViewById(R.id.button_reduce);
        final SeekBar seekBar = findViewById(R.id.seekBar);
        final SeekBar seekBar2 = findViewById(R.id.seekBar2);

        final Spinner spinner = findViewById(R.id.spinner);
        final String[] spinnerStrings = new String[]{"progressBar", "seekBar", "seekBar2"};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,
                spinnerStrings);
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentWidget = spinnerStrings[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        final ProgressBar progressBar = findViewById(R.id.progressBar);
        progressBar.setMax(100);
        increaseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (currentWidget) {
                    case "progressBar":
                        progressBar.setProgress(progressBar.getProgress() + 1);
                        break;
                    case "seekBar":
                        seekBar.setProgress(seekBar.getProgress() + 1);
                        break;
                    case "seekBar2":
                        if (seekBar2.getProgress() == 40) {
                            seekBar2.setProgress(80);
                        } else {
                            seekBar2.setProgress(seekBar2.getProgress() + 1);
                        }
                        break;
                }
            }
        });

        reduceBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentWidget.equals("progressBar")) {
                    progressBar.setProgress(progressBar.getProgress() - 1);

                } else if (currentWidget.equals("seekBar")) {
                    seekBar.setProgress(seekBar.getProgress() - 1);
                } else if (currentWidget.equals("seekBar2")) {
                    seekBar2.setProgress(seekBar2.getProgress() - 1);
                }
            }
        });


    }
}
